package com.tools.cleanmaster.scenenew;

import android.os.SystemClock;

import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.ui.notification.NotificationBaseItem;
import com.tools.cleanmaster.ui.notification.item.BatterySharpDecItem;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * 电量消耗过快场景点辅助类，单例
 */
public class BatterySharpDecHelper {
    private LinkedList<BatteryData> mBatteryDatas = new LinkedList<BatteryData>();

    private static BatterySharpDecHelper mInstance;

    private BatterySharpDecHelper() {
    }

    public static BatterySharpDecHelper getInstance() {
        if (mInstance == null) {
            synchronized (BatterySharpDecHelper.class) {
                if (mInstance == null) {
                    mInstance = new BatterySharpDecHelper();
                }
            }
        }
        return mInstance;
    }

    public static class BatteryData {
        public int percent;
        public long time;

        public BatteryData(int percent, long time) {
            this.percent = percent;
            this.time = time;
        }
    }

    public boolean batterySharpDec(BatteryInfoTracker.BatteryInfo newData) {
        boolean validFlag = false;
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(NotificationBaseItem.TAG, "开始判断电量消耗过快场景化，当前电量" + newData.percent + ", 已记录次数" + mBatteryDatas.size());
        }


        int percent = newData.percent;
        if (mBatteryDatas.isEmpty()) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(NotificationBaseItem.TAG, "首次判断，记录电量");
            }
            mBatteryDatas.add(new BatteryData(percent, SystemClock.elapsedRealtime()));
        } else if (percent != mBatteryDatas.getLast().percent) {
            long curTime = SystemClock.elapsedRealtime();

            String jsonBody = SceneSharedPrefManager.getSingleSceneConfigData(SceneType.BAT_SHARPDEC);
            int thresholdTime = BatterySharpDecItem.DEFAULT_TIME_MIN;
            int thresholdPercent = BatterySharpDecItem.DEFAULT_PERCENT;
            try {
                JSONObject jsonObject = new JSONObject(jsonBody);
                thresholdTime = jsonObject.optInt(BatterySharpDecItem.JSON_KEY_TIME, BatterySharpDecItem.DEFAULT_TIME_MIN);
                thresholdPercent = jsonObject.optInt(BatterySharpDecItem.JSON_KEY_PERCENT, BatterySharpDecItem.DEFAULT_PERCENT);
            } catch (JSONException e) {
                if (FeatureConfig.DEBUG_LOG) {
                    e.printStackTrace();
                }
            }

            while (!mBatteryDatas.isEmpty()) {
                if (curTime - mBatteryDatas.getFirst().time > thresholdTime * SceneConstants.MINUTE_MS) {
                    mBatteryDatas.removeFirst();
                } else {
                    break;
                }
            }
            for (BatteryData data : mBatteryDatas) {
                if (data.percent - percent >= thresholdPercent) {
                    if (FeatureConfig.DEBUG_LOG) {
                        LogHelper.d(NotificationBaseItem.TAG, "电量条件满足，判断其他条件");
                    }
                    mBatteryDatas.clear();
                    validFlag = true;
                    break;
                } else if (FeatureConfig.DEBUG_LOG) {
                    LogHelper.d(NotificationBaseItem.TAG, "电量差不满足阀值条件, 当前电量" + data.percent + " 记录电量 " + percent);
                }
            }
            mBatteryDatas.add(new BatteryData(newData.percent, curTime));
        } else if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(NotificationBaseItem.TAG, "当前电量和记录电量相等，不做判断");
        }
        return validFlag;
    }
}
